import java.util.*;


public class Solution2 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param grid int整型二维数组 
     * @return int整型
     */
    public int[][] grid;
    public int maxSum;
    public int maxValue (int[][] _grid) {
        // write code here
        grid = _grid;
        maxSum = 0;
        dfs(0, 0,0);
        return maxSum;
    }

    public void dfs(int sum, int x, int y) {
        //sum += grid[x][y];
        if(x == grid.length - 1 && y == grid[0].length -1 ) {
            maxSum = Math.max(maxSum, sum + grid[x][y]) ;
            return;
        }
        if(x + 1 < grid.length && y < grid[0].length) {
            dfs(sum + grid[x][y], x + 1, y);
        }
        if(y + 1 < grid[0].length && x < grid.length) {
            dfs(sum + grid[x][y], x, y + 1);
        }

//        if(x >= grid.length || y >= grid[0].length) {
//            return;
//        }
//        if(x == grid.length - 1 && y == grid[0].length - 1) {
//            maxSum = Math.max(maxSum, sum + grid[x][y]);
//            return;
//        }
//        dfs(sum + grid[x][y], x + 1, y);
//        dfs(sum + grid[x][y], x, y + 1);
    } 
}